package com.tutu.controller;

import com.tutu.entity.Image;
import com.tutu.entity.vo.BaseCodeVo;
import com.tutu.entity.vo.BaseVo;
import com.tutu.entity.vo.CodeDictVo;
import com.tutu.entity.vo.EditImg;
import com.tutu.service.*;
import com.tutu.utils.BaseUtil;
import com.tutu.utils.DateUtils;
import com.tutu.utils.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 公共方法控制层
 * Created by 涂奕恒 on 2016/10/18.
 */
@Controller
@RequestMapping(value = "common")
public class CommonsController {

    // 创建可缓存的无界线程池
    public static ExecutorService executorService = Executors.newCachedThreadPool();

    public static List<Map<String, Object>> baseRoleList = new ArrayList<>();

    public static List<Map<String, Object>> baseCompanyList = new ArrayList<>();

    public static Map<String, List<Map<String, Object>>> baseDeviceList = new HashMap<>();

    public static List<Map<String, Object>> baseUserList = new ArrayList<>();

    public static Map<String, CodeDictVo> baseCodeDictMap = new HashMap<>();

    public static Map<String, List<CodeDictVo>> deviceCodeDictMap = new HashMap<>();

    // 同步时间记录
    public static Map<String, Long> syncTimeMap = new HashMap<>();

    // 当前网站菜单信息
    public static List<BaseCodeVo> loginMenuList = new ArrayList<>();

    @Autowired
    DeviceService deviceService;

    @Autowired
    UserService userService;

    @Autowired
    CodeDictService codeDictService;

    @Autowired
    EmissionCodeService emissionCodeService;

    @Autowired
    InterfaceService interfaceService;

    @RequestMapping(value = "/codeDictTypeList")
    @ResponseBody
    public List<BaseVo> codeDictTypeList() {
        return codeDictService.findTypeList();
    }

    @RequestMapping(value = "/emissionCodeListByType")
    @ResponseBody
    public List<BaseCodeVo> emissionCodeListByType(Integer type) {
        return emissionCodeService.findListByType(type);
    }

    @RequestMapping(value = "/emissionCodeListByDeviceId")
    @ResponseBody
    public List<BaseCodeVo> emissionCodeListByDeviceId(Integer deviceId) {
        return emissionCodeService.findListByDeviceId(deviceId);
    }

    @RequestMapping(value = "/roleList")
    @ResponseBody
    public List<BaseCodeVo> roleList() {
        return BaseUtil.changeInfo(CommonsController.baseRoleList);
    }

    @RequestMapping(value = "/companyList")
    @ResponseBody
    public List<BaseCodeVo> companyList() {
        return BaseUtil.changeInfo(CommonsController.baseCompanyList);
    }

    @RequestMapping(value = "/deviceList")
    @ResponseBody
    public List<BaseCodeVo> deviceList(Integer companyId) {
        return deviceService.findListByCompanyId(companyId);
    }

    @RequestMapping(value = "/userList")
    @ResponseBody
    public List<BaseVo> userList(Integer companyId) {
        return userService.findListByCompanyId(companyId);
    }

    @RequestMapping(value = "/codeDictList")
    @ResponseBody
    public List<CodeDictVo> codeDictList(String deviceCode, Integer type) {
        return codeDictService.findDataList(deviceCode, type);
    }

    /**
     * 上传图片
     */
    @RequestMapping("/uploadImage")
    @ResponseBody
    public Image uploadImage(MultipartRequest multipartRequest) {
        return ImageUtils.saveImage(multipartRequest);
    }

    /**
     * 刷新基础数据
     */
    @RequestMapping("/refreshBaseInfo")
    @ResponseBody
    public Map<String, Object> refreshBaseInfo(HttpServletRequest request) {
        return interfaceService.refreshBaseInfo(request);
    }

    // 检测是否允许同步数据
    public static boolean needSync(String key) {
        if (!syncTimeMap.containsKey(key)) {
            syncTimeMap.put(key, System.currentTimeMillis());

            return true;
        } else {
            // 超过二十分钟才允许再次同步
            return DateUtils.getWaitTime(syncTimeMap.get(key), System.currentTimeMillis()) > 0;
        }
    }
}
